package pms.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import pms.om.Kategorie;
import pms.om.Person;
import pms.om.PersonHatNoten;

public class PersonDao extends HibernateDaoSupport{
	private PersonHatNotenDao personHatNoten;

	/** Getter
	 * @return the personHatNoten
	 */
	public PersonHatNotenDao getPersonHatNoten() {
		return personHatNoten;
	}

	/** Setter
	 * @param personHatNoten the personHatNoten to set
	 */
	public void setPersonHatNoten(PersonHatNotenDao personHatNoten) {
		this.personHatNoten = personHatNoten;
	}

	/** findById
	 * @param id
	 * @return the Person
	 */
	public Person findById (int id) {
		HibernateTemplate template = getHibernateTemplate();
		return (Person) template.get(Person.class, id);
	}
	
	/** findPersonHatNotenByPersonId
	 * @param id
	 * @return the foundPersonHatNoten
	 */
	@SuppressWarnings("unchecked")
	public List<PersonHatNoten> findPersonHatNotenByPersonId(int id) {
		HibernateTemplate template = getHibernateTemplate();
		List<PersonHatNoten> allePersonHatNoten = template.loadAll(PersonHatNotenDao.class);
		List<PersonHatNoten> foundPersonHatNoten = new ArrayList<PersonHatNoten>();
		for (PersonHatNoten personHatNoten : allePersonHatNoten) {
			if (personHatNoten.getNoten().getId() == id) {
				foundPersonHatNoten.add(personHatNoten);
			}
		}
		return foundPersonHatNoten;
	}
	
	/** findAll
	 * @return the results
	 */
	@SuppressWarnings("unchecked")
	public List<Person> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		List<Person> results = template.loadAll(Person.class);
		return results;
	}
	
	/** create
	 * @param nachname
	 * @param vorname
	 * @param ort
	 * @param plz
	 * @param strasse
	 * @param emailadresse
	 * @param telefon
	 */
	public void create(String nachname, String vorname, String ort,
			String plz, String strasse, String emailadresse, String telefon) {
		HibernateTemplate template = getHibernateTemplate();
		Person p = new Person(nachname, vorname, ort, plz, strasse, emailadresse, telefon);
		template.persist(p);
	}
	
	/** modify
	 * @param id
	 * @param nachname
	 * @param vorname
	 * @param ort
	 * @param plz
	 * @param strasse
	 * @param emailadresse
	 * @param telefon
	 */
	public void modify(int id, String nachname, String vorname, String ort,
			String plz, String strasse, String emailadresse, String telefon) {
		HibernateTemplate template = getHibernateTemplate();
		Person p = (Person) template.get(Person.class, id);
		p.setNachname(nachname);
		p.setVorname(vorname);
		p.setOrt(ort);
		p.setPlz(plz);
		p.setStrasse(strasse);
		p.setEmailadresse(emailadresse);
		p.setTelefon(telefon);
		template.saveOrUpdate(p);
	}
	
	/** deleteById
	 * @param id
	 * @throws Exception PersonDeleteExeption
	 */
	public void deleteById(int id)throws Exception {
		HibernateTemplate template = getHibernateTemplate();
		Person p = findById(id);
		List<PersonHatNoten> allePersonHatNoten = findPersonHatNotenByPersonId(p.getId());
		if (allePersonHatNoten == null || allePersonHatNoten.size() == 0) {
			template.delete(p);
		} else {
			throw new PersonDeleteExeption();
		}
		template.flush();
	}
}
